package com.cqs.leetcode;

import java.util.Stack;

/**
 * Created by cqs on 2018/3/31.
 */
public class ValidParentheses {

    public boolean isValid(String s) {
        if (s == null || s.length() <= 1) return false;
        Stack<Character> stack = new Stack<>();
        for (int i = 0 ; i < s.length();++i) {
            char c = s.charAt(i);
            switch (c) {
                case ')':
                    if (stack.isEmpty() || stack.pop() != '(') return false;
                    break;
                case ']':
                    if (stack.isEmpty() || stack.pop() != '[') return false;
                    break;
                case '}':
                    if (stack.isEmpty() || stack.pop() != '{') return false;
                    break;
                default:
                    stack.push(c);
            }
        }
        return stack.isEmpty();
    }

    public static void main(String[] args) {
        ValidParentheses v = new ValidParentheses();
        System.out.println(v.isValid("{"));
        System.out.println(v.isValid("}}}}}}}}}"));
        System.out.println(v.isValid("()[]{}"));
        System.out.println(v.isValid("(]"));
        System.out.println(v.isValid("([)]"));
    }
}
